<?php


namespace App\Modules\Health\Http\Controllers\Api;


use App\Http\Controllers\Api\Controller;
use App\Modules\Health\Constants\AcuConfigConstant;
use App\Modules\Health\Models\Acupoint;
use App\Modules\Health\Models\Body;
use App\Modules\Health\Models\HealthTedingAcupoint;
use App\Modules\Health\Models\HealthWushuAcupoint;
use App\Modules\Health\Models\Meridian;
use App\Modules\Health\Services\ConfigService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class AcuController extends Controller
{
    public function index(Request $request)
    {
        $configs = app(ConfigService::class)->getConfigs(AcuConfigConstant::ACU_GROUP);
        $classType = $configs[AcuConfigConstant::SHOW_ACU_CLASS_TYPE] ?? 0;

        $data = [
            'class_type' => $classType,
        ];

        if ($classType) {
            $wId = array_filter(explode(',', $request->input('w_id', '')));
            $tId = array_filter(explode(',', $request->input('t_id', '')));
            $bId = array_filter(explode(',', $request->input('b_id', '')));

            $meridian = Meridian::with(['acu' => function ($q) use ($wId, $tId, $bId) {
                return $q->select(['id', 'name', 'point_img as img'])
                    ->when($wId, function ($q, $wId) {
                        $q->whereIn('wushu_id', $wId);
                    })->when($tId, function ($q, $tId) {
                        $q->whereIn('teding_id', $tId);
                    })->when($bId, function ($q, $bId) {
                        $q->whereHas('body', function ($q) use ($bId) {
                            $q->whereIn('id', $bId);
                        });
                    });
            }])->where('status', true)
                ->when($wId, function ($q, $wId) {
                    $q->whereHas('acu', function ($q) use ($wId) {
                        return $q->whereIn('wushu_id', $wId);
                    });
                })
                ->when($tId, function ($q, $tId) {
                    $q->whereHas('acu', function ($q) use ($tId) {
                        return $q->whereIn('teding_id', $tId);
                    });
                })
                ->when($bId, function ($q, $bId) {
                    $q->whereHas('acu', function ($q) use ($bId) {
                        return$q->whereHas('body', function ($q) use ($bId) {
                            $q->whereIn('id', $bId);
                        });
                    });
                })
                ->select(['id', 'name', 'abbr_name', 'time_desc'])
                ->get();

            $meridian->each(function ($item) {
                $item->ap = $item->abbr_name ? Str::substr($item->abbr_name, 0, 1) : Str::substr($item->name, -2, 1);
            });

            $data['ap'] = $meridian->pluck('ap')->all();

            $body = [];

            $data['search'] = [
                ['key' => 'w_id', 'name' => '五输穴', 'data' => HealthWushuAcupoint::get(['id', 'name'])],
                ['key' => 't_id', 'name' => '特定穴', 'data' => HealthTedingAcupoint::get(['id', 'name'])],
                ['key' => 'b_id', 'name' => '部位', 'data' => Body::where('status', 1)->get(['id', 'name'])],
            ];
        } else {
            $meridian = Meridian::query()
                ->where('status', true)
                ->select(['id', 'name', 'img'])
                ->get();

            $body = Body::query()
                ->where('status', true)
                ->select(['id', 'name', 'img'])
                ->get();

            $meridian->first->load('acu:id,name,point_img as img');
            $body->first->load('acu:id,name,point_img as img');
        }

        $data['meridian'] = $meridian;
        $data['body'] = $body;

        return $this->_success($data);
    }

    public function item(Request $request)
    {
        $data = Validator::make($request->input(), [
            'type'    => 'required|in:meridian,body',
            'item_id' => 'required|integer',
        ], [
            'type.required'    => 'type不能为空',
            'type.in'          => 'type参数错误',
            'item_id.required' => 'item_id不能为空',
            'item_id.integer'  => 'item_id类型错误',
        ])->validate();

        $info = $this->{$data['type']}($data['item_id']);
        if (!$info) {
            return $this->error('信息不存在');
        }

        return $this->_success([
            'type' => $data['type'],
            'info' => $info,
        ]);
    }

    public function info(int $id)
    {
        $info = Acupoint::query()->find($id);
        if (!$info) {
            return $this->error('信息不存在');
        }

        return $this->_success(compact('info'));
    }

    public function search(Request $request)
    {
        $keyword = $request->input('keyword');
        if (!$keyword) {
            return $this->error('关键词不能为空');
        }

        $list = Acupoint::select(['id', 'name', 'point_img as img'])
            ->where('name', 'like', '%' . $keyword . '%')
            ->get();

        return $this->_success(compact('list'));
    }

    protected function meridian(int $id)
    {
        return Meridian::query()->select(['id', 'name', 'img'])->where('status', '=', true)->with('acu:id,name,point_img as img')->find($id);
    }

    protected function body(int $id)
    {
        return Body::query()->select(['id', 'name', 'img'])->where('status', '=', true)->with('acu:id,name,point_img as img')->find($id);
    }
}
